import ffmpy
import os


def change_video(file, ctype):
    """
    视频转换
        :param file: 文件名
        :param ctype: 需要转换的类型
        :return:
    """
    if os.path.exists(file):

        ff = ffmpy.FFmpeg(
            inputs={file: None},
            outputs={edit_file(file, ctype=ctype): '-ac 1 -ar 16000'}
        )

        print(ff.cmd)
        return ff.run()
    else:
        return False


def divide_video(file, time_seq):
    """
    视频分割
        :param file: 文件名
        :param time_seq: 要分割的时间序列
        :return:
    """
    pathname, filename, typename = get_info_path(file)
    pathname += filename + '/'
    if os.path.exists(file) and make_folder(pathname):
        start_time = 0.0
        for i in range(len(time_seq)):
            end_time = time_seq[i]
            mini_name = edit_file(file, cname=('音频' + str(i + 1)), cpath=pathname)
            divide_time_str = "-ss " + str(start_time) + " -t " + str(end_time-start_time)
            start_time = end_time

            ff = ffmpy.FFmpeg(
                inputs={file: divide_time_str},
                outputs={mini_name: '-vcodec copy'}
            )
            print(ff.cmd)
            ff.run()

        return True
    else:
        return False


def load_subtitle(subtitle_path, file_path):
    if os.path.exists(subtitle_path) and os.path.exists(file_path):

        params = '-i ' + subtitle_path + " -c:s mov_text -c:v copy -c:a copy"

        ff = ffmpy.FFmpeg(
            inputs={file_path: None},
            outputs={edit_file(file_path, cname='output'): params}
        )

        print(ff.cmd)
        return ff.run()
    else:
        return False


def edit_file(file, cpath=None, cname=None, ctype=None):
    """
    修改文件名
        :param file: 原文件名
        :param cpath: 要修改的路径
        :param cname: 要修改的名字
        :param ctype: 要修改的类型
        :return:
    """
    if file:
        array = file.split('/')

        pathname = ''
        for i in range(0, len(array) - 1):
            pathname += array[i] + '/'
        filename = array[-1]

        array = filename.split('.')
        filename = ''
        for i in range(0, len(array)-1):
            filename += array[i]
            if i != len(array)-2:
                filename += '.'
        typename = array[-1]

        # 开始变换
        if cpath:
            pathname = cpath
        if cname:
            filename = cname
        if ctype:
            typename = ctype

        file = pathname + filename + '.' + typename
    return file


def make_folder(file):
    """
    创建文件夹
        :param file:
        :return:
    """
    flag = os.path.exists(file)

    if not flag:
        os.makedirs(file)
        return True
    else:
        return False


def get_info_path(path):
    """
    根据文件路径获取信息
        :param path:
        :return:
    """
    if path:
        array = path.split('/')

        pathname = ''
        for i in range(0, len(array) - 1):
            pathname += array[i] + '/'
        filename = array[-1]

        array = filename.split('.')
        filename = ''
        for i in range(0, len(array)-1):
            filename += array[i]
            if i != len(array)-2:
                filename += '.'
        typename = array[-1]

    return pathname, filename, typename
